<?php
$loader->requireOnce('includes/GeneralReport/PortletGReport.class.php');
class PortletGReportLineHollow extends PortletGReport {

	public function __construct($reportId){
		parent::__construct($reportId);
	}

	protected function collectData() {
		// STEP 1: Get report structure information - name, unit,..
		$sql		=	  "SELECT * FROM general_report WHERE general_report_id='$this->reportId'";
		$res		=	  $this->_db->GetAll($sql);
		$report     =     array();
		if(($res!= false) && (count($res) > 0)) {
			$report  =   $res[0];
			$yLengend    =   $report['data_name'];
            if($report['data_type']=='currency'){
                $yLengend   .=  " (ĐVT: ". $report['data_unit'] .")";
            }
            $this->y_legend =   $yLengend;
            $this->name     =   $report['name'];
		}
		else {
			$this->y_legend    =   "Dữ liệu";
			$this->x_legend    =   "Biến";
            $this->name        =   "Báo cáo ID=$this->reportId không có dữ liệu";
		}

		// STEP 2: Get report data
		$sql = "SELECT * FROM general_report_data WHERE report_id='$this->reportId'
				ORDER BY general_report_data.order";
		$result = $this->_db->GetAll($sql);
		if(count($report) > 0){
			$i = 0;
			if($report['data_type'] == 'number') {
				foreach ($result as $row){
					$color = '#3030D0';
					// Remember to use INTEGER data for the chart value (string will not be rendered)
                    // Chart value
                    $val    =   (int)($row['data_value']);
                    $this->values[$i] = new OFC_Charts_Line_Value($val, $color);
					// X-label
                    $this->xLabels[$i] = $report['metric_name'] . " " . $row['metric_value'];
                    // Max value
                    if($this->maxValue < $val) {
                        $this->maxValue =   $val;
                    }
                    //Tip for value
                    $tip    =   $report['data_name']. ": ". number_format($row['data_value'], 0, ',', '.')
                         . "<br>". $report['metric_name'] . " " . $row['metric_value'];
                    $this->values[$i]->set_tooltip($tip);
					$i++;
				}
			}
			else {
			 foreach ($result as $row){
                   $color = '#3030D0';
                    // Remember to use INTEGER data for the chart value (string will not be rendered)
                    // Chart value
                    $val    =   (int)($row['data_value']);
                    $this->values[$i] = new OFC_Charts_Line_Value($val, $color);
                    // X-label
                    $this->xLabels[$i] = $report['metric_name'] . " " . $row['metric_value'];
                    // Max value
                    if($this->maxValue < $val) {
                        $this->maxValue =   $val;
                    }
                    //Tip for value
                    $tip    =   $report['data_name']. ": ". number_format($row['data_value'], 0, ',', '.')
                                . " (ĐVT: ". $report['data_unit'] .")"
                                . "<br>". $report['metric_name'] . " " . $row['metric_value'];
                    $this->values[$i]->set_tooltip($tip);
                    $i++;
                }
			}
		}
	}

	protected function buildChart() {
		// Create chart object and set backgound color
		$this->chart = new OFC_Chart();
		$this->chart->set_bg_colour("#FEFEFE");
		// Create chart title object and pass css param
		$title = new OFC_Elements_Title($this->name);
		$title->set_style("font-size: 13px; font-weight: bold; padding-bottom: 10px; color: #00248E");
		// Create chart object (each type is an object)
		$line_hollow = new OFC_Charts_Line_Hollow();
		// Set chart value
		$line_hollow->set_values($this->values);
		$line_hollow->set_halo_size(1);
		// Set lable, style for X axis
		$x_axis = new OFC_Elements_Axis_X();
    	$x_axis->set_labels_from_array($this->xLabels);
		$x_axis->labels->set_rotate('diagonal');
		$x_axis->labels->set_size(9);
		$x_axis->set_colours("#555555", "#DDDDDD");
		$x_legend = new OFC_Elements_Legend_X($this->x_legend);
        $x_legend->set_style("font-size: 12px;");
		// Set value range, step and style for Y axis
		$y_axis   =   new OFC_Elements_Axis_Y();
		$maxY     =   self::getMaxY();
		$stepY    =   self::getStepY();
		$y_axis->set_range(0, $maxY, $stepY);
		$y_axis->set_colours("#555555", "#DDDDDD");
		$y_legend = new OFC_Elements_Legend_Y($this->y_legend);
		$y_legend->set_style("font-size: 12px;");

		// Add all of them to chart object
		$this->chart->set_title($title);
		$this->chart->add_element($line_hollow);
		$this->chart->set_x_axis($x_axis);
		$this->chart->set_y_axis($y_axis);
		$this->chart->set_x_legend($x_legend);
		$this->chart->set_y_legend($y_legend);
	}
}
?>